
#include "comm.hpp"

int main()
{
    std::cout<<"test server"<<std::endl;
    //获取key值
    key_t key = GetKey();
    printf("key: 0x%x\n",key);
    //创建共享内存
    int shmid = CreateShm(key);
    std::cout<<"shmid: "<<shmid<<std::endl;

    //挂接
    char* str = (char*)AttachShm(shmid);

    creatnamepipe();
    int fd = open(NAME_PIPE, O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        exit(1);
    }

    int buf;
    //接收消息
    while(true)
    {
        int ret = read(fd, &buf, 4);
        if(ret ==0)
        {
            std::cout<<"写端关闭"<<",退出读取循环"<<std::endl;
            break;
        }
        
        std::cout<<"受到开始读取信号："<<buf<< std::endl;
        printf("clinet say；%s\n", str);
         //sleep(1);
    }


    delnamepipe();
    //去挂接
    DetachShm(str);
    //删除共享内存
    DelShm(shmid);
    return 0;
}